<?php
//包含数据库配置文件
include '../public/dbconfig.php';
//包含函数库文件
include '../public/func.inc.php';
//调用函数
$link = conn();
//var_dump($link);
//exit;
$username = $_POST['uname'];
$password = md5($_POST['pass']);
var_dump($username);
//var_dump($password);

//查询是否有此用户
$sql = "select id,username,password,pic,tel,qx from user where username='{$username}'";

//var_dump($sql);
//执行查询
$res = mysqli_query($link, $sql);
//获取查询结果条数 没有结果为0
//echo mysqli_num_rows($res);
//var_dump($res);
//exit;
if($res && mysqli_num_rows($res) > 0){
	$uinfo = mysqli_fetch_assoc($res);
	//用户名有 对比密码
	if($password == $uinfo['password']){
		//var_dump($password);
		
		//密码正确 验证权限
		if($uinfo['qx'] == 2){
			//var_dump($uinfo['qx']);
			//exit;
			//有权限 登录成功 存储信息
			setcookie('isLogin',1,time()+3600*24,'/project/');
			setcookie('uid',$uinfo['id'],time()+3600*24,'/project/');
			setcookie('uname',$uinfo['username'],time()+3600*24,'/project/');
			setcookie('pass',$uinfo['password'],time()+3600*24,'/project/');
			setcookie('qx',$uinfo['qx'],time()+3600*24,'/project/');
			setcookie('pic',$uinfo['pic'],time()+3600*24,'/project/');
			setcookie('tel',$uinfo['tel'],time()+3600*24,'/project/');
			setcookie('edu',$uinfo['edu'],time()+3600*24,'/project/');
			setcookie('sign',$uinfo['sign'],time()+3600*24,'/project/');
			//跳转到后台首页
			//header('location:./index.php');
			echo '<script>alert("恭喜，登录成功");window.location.href="index.php";</script>';
		}else{
			//没有权限
			//header('location:./login.php?error=3');
			echo '<script>alert("对不起,您的权限不足");window.location.href="login.php?error=3";</script>';
		}
	}else{
		//密码错误
		//header('location:./login.php?error=2');
		echo '<script>alert("对不起，你的密码错误,请重新输入");window.location.href="login.php?error=2";</script>';
	}
	//释放结果集资源 
	mysqli_free_result($res);
}else{
	//用户名错误
	//header('location:./login.php?error=1');
	echo '<script>alert("对不起,您的用户名不存在,请注册");window.location.href="login.php?error=1";</script>';
}

//关闭数据库连接
mysqli_close($link);

?>